import { Controller, Get, Post, Body, Param, Put, Delete, HttpCode, Query } from '@nestjs/common';
import { VideoCategoryService } from './video-category.service';
import { VideoCategory } from 'src/entities/video-category.entity';
import { CreateVideoCategoryDto } from './dto/create-video-category.dto';
import { UpdateVideoCategoryDto } from './dto/update-video-category.dto';
import { Video } from 'src/entities/video.entity';
import { 
  ApiTags, 
  ApiOperation, 
  ApiParam, 
  ApiQuery, 
  ApiResponse, 
  ApiBody
} from '@nestjs/swagger';
import { Public } from 'src/common/decorators/public';
import { QueryVideoCategoryDto } from './dto/query-video-category.dto';

@ApiTags('视频分类')
@Controller('video-categories')
export class VideoCategoryController {
  constructor(private readonly videoCategoryService: VideoCategoryService) {}

  @ApiOperation({ summary: '获取所有视频分类' })
  @Get()
  @Public()
  async findAll(): Promise<VideoCategory[]> {
    return await this.videoCategoryService.findAll();
  }

  @ApiOperation({ summary: '分页获取视频分类列表' })
  @ApiQuery({ type: QueryVideoCategoryDto })
  @Get('page')
  @Public()
  async findByPage(@Query() query: QueryVideoCategoryDto): Promise<{
    items: VideoCategory[];
    total: number;
    page: number;
    pageSize: number;
    totalPages: number;
  }> {
    return await this.videoCategoryService.findByPage(query);
  }
  
  @ApiOperation({ summary: '获取视频分类options' })
  @Get('options')
  @Public()
  async getCategoryOptions(): Promise<{ value: string; label: string }[]> {
    return await this.videoCategoryService.getCategoryOptions();
  }

  @ApiOperation({ summary: '根据ID获取单个视频分类' })
  @ApiParam({ name: 'id', description: '视频分类ID' })
  @Get(':id')
  async findOne(@Param('id') id: number): Promise<VideoCategory> {
    return await this.videoCategoryService.findOne(id);
  }

  @ApiOperation({ summary: '获取指定分类下的视频列表' })
  @ApiParam({ name: 'id', description: '视频分类ID' })
  @ApiQuery({ name: 'page', description: '页码', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', description: '每页条数', required: false, type: Number })
  @ApiQuery({ 
    name: 'lastId', 
    description: '最后一条记录的ID（用于游标分页）', 
    required: false, 
    type: Number 
  })
  @Get(':id/videos')
  async getCategoryVideos(
    @Param('id') categoryId: number,
    @Query('page') page: number = 1,
    @Query('pageSize') pageSize: number = 10,
    @Query('lastId') lastId?: number,
  ): Promise<{ videos: Video[]; hasMore: boolean; total: number }> {
    return await this.videoCategoryService.getCategoryVideos(categoryId, {
      page,
      pageSize,
      lastId,
    });
  }

  @ApiOperation({ summary: '创建新的视频分类' })
  @ApiBody({ 
    type: CreateVideoCategoryDto,
    description: '视频分类创建数据'
  })
  @ApiResponse({ 
    status: 201, 
    description: '分类创建成功',
    type: VideoCategory
  })
  @ApiResponse({ status: 409, description: '分类名称已存在' })
  @Post()
  async create(@Body() createVideoCategoryDto: CreateVideoCategoryDto): Promise<VideoCategory> {
    return await this.videoCategoryService.create(createVideoCategoryDto);
  }

  @ApiOperation({ summary: '更新视频分类' })
  @ApiParam({ name: 'id', description: '视频分类ID' })
  @ApiBody({ 
    type: UpdateVideoCategoryDto,
    description: '视频分类更新数据'
  })
  @ApiResponse({ 
    status: 200, 
    description: '分类更新成功',
    type: VideoCategory
  })
  @ApiResponse({ status: 404, description: '分类不存在' })
  @ApiResponse({ status: 409, description: '分类名称已存在' })
  @Put(':id')
  async update(
    @Param('id') id: number,
    @Body() updateVideoCategoryDto: UpdateVideoCategoryDto,
  ): Promise<VideoCategory> {
    return await this.videoCategoryService.update(id, updateVideoCategoryDto);
  }

  @ApiOperation({ summary: '删除视频分类' })
  @ApiParam({ name: 'id', description: '视频分类ID' })
  @ApiResponse({ status: 200, description: '分类删除成功' })
  @ApiResponse({ status: 403, description: '分类不存在' })
  @Delete(':id')
  async remove(@Param('id') id: number): Promise<void> {
    return await this.videoCategoryService.remove(id);
  }
}